// components/orderSheet/unpaidOrderItem.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //订单编号
    orderNo: {
      type: String,
      value: '20210715205309'
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPath: '/images',
    //剩余时间
    leftTime: '已完成',
    //结束时间
    endTime: '2022-11-17',
    orderItems: [{
      itemId: '1',
      itemNum: 1,
      itemImgSrc: '/cart/item.jpg',
      itemName: 'MAGIC DRAGON魔杰龙轻柔倍适学步裤',
      itemDescrition: '尺寸：L',
      itemPrice: '138.00'
    }, {
      itemId: '2',
      itemNum: 1,
      itemImgSrc: '/cart/item.jpg',
      itemName: 'MAGIC DRAGON魔杰龙轻柔倍适学步裤',
      itemDescrition: '尺寸：L',
      itemPrice: '138.00'
    }],
    //价钱合计
    totalPrice: 0,

    //详情
    detail: null,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 整数小于10开头加0
     * @param {Number} d 数值
     */
    addZeroHead(d) {
      if (d < 10) return '0' + d;
      return d;
    },
    /**
     * 计算总价
     */
    calTotalPrice() {
      // let num = 0.0;
      // for (let item of this.data.orderItems) {
      //   num += Number(item.itemPrice);
      // }
      // console.log("num", num)
      this.setData({
        // totalPrice: num.toFixed(2)
        totalPrice: this.data.detail.price,
      })
    },
    /**
     * 取消订单
     * @param {*} e 
     */
    applyService(e) {
      this.triggerEvent("applyService", this.data.orderNo)
    },
    /**
     * 立即支付
     * @param {*} e 
     */
    buyAgain(e) {
      this.triggerEvent("buyAgain", this.data.orderNo)
      let goods = this.data.detail.goods_info[0];
      if (goods) {
        if(goods.goods_type == 100 ){
          wx.navigateTo({
            url: '/pages/plan/joinPlan',
          })
        }else{
          wx.navigateTo({
            url: '/pages/goods/detail?goods_id=' + goods.goods_id,
          })
        }
      }else{
        wx.showToast({
          title:'商品不存在',
        })
      }
    },
    /**
     * 处理详情
     * @param {*} data 
     */
    handleDetail(data) {
      const that = this;

      //订单项
      let orderItems = [];
      for (const item of data.goods_info) {
        orderItems.push({
          itemId: item.goods_id,
          itemNum: item.buy_number,
          itemImgSrc: item.image,
          itemName: item.goods_name,
          itemDescrition: item.stock_info.spec_value_name,
          itemPrice: item.price,
        })
      }

      that.setData({
        endTime: data.complate_pay_time,
        orderItems: orderItems,
      })

      //设置一个定时器，计算付款剩余时间
      // that.calLeftTime();

      //计算总价
      that.calTotalPrice()

      // console.log("completedOrderItem,orderDetail,that.data", that.data)
    },
  },
  lifetimes: {
    attached() {
      const that = this;

      // console.log("completedOrderItem,attached");

      //图片路径
      const util = require("../../utils/util")
      this.setData({
        imgPath: util.getImgPath()
      })

      //从缓存取出orderList，如果有，直接使用
      let orderList = wx.getStorageSync('orderList')
      // console.log("orderList", orderList)
      if (!orderList) {

        //取订单详情
        const api = require("../../api/api")
        api.user.orderDetail({
          order_sn: this.data.orderNo,
          success(res) {
            // console.log("orderDetail,res", res)

            let data = res.data.data;

            that.setData({
              detail: data,
            })

            //处理详情
            that.handleDetail(data);

            /*
            //订单项
            let orderItems = [];
            for (const item of data.goods_info) {
              orderItems.push({
                itemId: item.goods_id,
                itemNum: item.buy_number,
                itemImgSrc: item.image,
                itemName: item.goods_name,
                itemDescrition: item.stock_info.spec_value_name,
                itemPrice: item.price,
              })
            }

            that.setData({
              endTime: data.complate_pay_time,
              orderItems: orderItems,
            })

            //设置一个定时器，计算付款剩余时间
            // that.calLeftTime();

            //计算总价
            that.calTotalPrice()

            console.log("completedOrderItem,orderDetail,that.data", that.data)
            */
          }
        })
      } else {
        let data;
        for (let item of orderList) {
          if (item.order_sn == this.data.orderNo) {
            data = item;
            break;
          }
        }

        //保存详情数据
        that.setData({
          detail: data
        })

        //处理详情
        this.handleDetail(data);
      }

      //计算总价
      // this.calTotalPrice()
    }
  }
})